package net.cyue.simple.container.injector;

import net.cyue.simple.container.annotation.Inject;
import net.cyue.simple.container.data.InjectableMetaType;
import net.cyue.simple.container.data.MetaType;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class MetaTypeFactory {
    private static List<MetaType> getMetaTypeDependencies(Class<?> clz) {
        List<MetaType> dependencies = new ArrayList<>();
        Field[] fields = clz.getDeclaredFields();
        for (Field f : fields) {
            Inject injectAnnotation = f.getAnnotation(Inject.class);
            if (injectAnnotation == null) {
                continue;
            }
            MetaType mt = new MetaType(
                f.getClass(),
                MetaTypeFactory.getMetaTypeDependencies(f.getClass())
            );
            String name = Injector.getInjectName(injectAnnotation);
            InjectableMetaType iMT = new InjectableMetaType(mt, name);
            dependencies.add(iMT);
        }

        return dependencies;
    }


    public static MetaType createMetaType(Class<?> clz) {
        return new MetaType(
            clz,
            MetaTypeFactory.getMetaTypeDependencies(clz)
        );
    }

    public static InjectableMetaType createInjectableMetaType(Class<?> clz, String name) {
        MetaType mt = MetaTypeFactory.createMetaType(clz);
        return new InjectableMetaType(
            mt,
            name
        );
    }
}
